package org.liveboardgames.agricola.business.action.processor;

import java.util.List;
import java.util.Set;

import org.liveboardgames.agricola.business.helper.GameHelper;
import org.liveboardgames.agricola.business.updater.BoardTileUpdater;
import org.liveboardgames.agricola.domain.Change;
import org.liveboardgames.agricola.domain.Game;
import org.liveboardgames.agricola.domain.action.Action;
import org.liveboardgames.agricola.domain.action.ResourcesOnBoardAction;
import org.liveboardgames.agricola.domain.board.BoardTile;

public class AddResourceOnBoardActionProcessor extends ActionProcessor {

  public AddResourceOnBoardActionProcessor(Game game, Action action, List<Change> changes) {
    super(game, action, changes);
  }

  @Override
  public void execute() {
    // Get the parameters of the action
    if (action instanceof ResourcesOnBoardAction) {
      ResourcesOnBoardAction resourceAction = (ResourcesOnBoardAction)action;

      String resourceType = resourceAction.getResourceType();
      Set<Integer> roundNumbers = resourceAction.getRounds();
      int quantity = resourceAction.getQuantity();
      String roundOffsetType = resourceAction.getOffsetType();

      // Now we need to put the proper resources at the proper place on the
      // board
      // Compute the number of the rounds where to put the resource
      GameHelper gameHelper = new GameHelper();
      Set<Integer> realRoundNumbers = gameHelper.computeRealRoundNumbers(game, roundNumbers, roundOffsetType);

      // Add the resource on each tile
      for (Integer round : realRoundNumbers) {
        BoardTile boardTile = gameHelper.getTileForRound(game, round);
        BoardTileUpdater updater = new BoardTileUpdater(boardTile, changes);
        updater.addResource(resourceType, quantity);
      }
    }
  }

}
